package com.woniu.controller;

import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Doctor;
import com.woniu.service.IDoctorService;
import com.woniu.vo.DoctorDetailVo;
import com.woniu.vo.QuerySearchDoctorVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author doujiao
 * @since 2022-10-10
 */
@RestController
@RequestMapping("/doctor")
@Api(tags = "医生模块")
@Slf4j
public class DoctorController extends BaseController{


    @Autowired
    private IDoctorService doctorService;

    /**
     * 分页搜索查找所有医生
     */
    @ApiOperation("分页搜索查找所有医生")
    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result searchByPage(@PathVariable String currentPage, @PathVariable String pageSize,
                               @RequestBody QuerySearchDoctorVo searchDoctorVo){
       PageResult pageResult= doctorService.searchByPage(currentPage,pageSize,searchDoctorVo);
        System.out.println("111");
       return toDataResult(pageResult);
    }
    /**
     * 根据id查询一条数据
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询一条数据")
    public Result queryById(@PathVariable Integer id){
        //转Long
        Long id1 = Long.valueOf(id);
        Doctor doctor = doctorService.getById(id1);
        return toDataResult(doctor);
    }

    /**
     * 增加医生
     */
    @PostMapping
    @ApiOperation("增加医生")
    public Result add(@RequestBody Doctor doctor){
        boolean save = doctorService.save(doctor);
        return toResult(save);
    }

    /**
     * 修改医生
     */
    @PutMapping
    @ApiOperation("修改医生")
    public Result edit(@RequestBody Doctor doctor){
        boolean result = doctorService.updateById(doctor);
        return toResult(result);
    }
    /**
     * 根据id删除医生
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除医生")
    public Result removeById(@PathVariable Integer id){
        Long id1 = Long.valueOf(id);
        boolean result = doctorService.removeById(id1);
        return toResult(result);
    }

    /**
     * 登录
     */
    @ApiOperation("模拟登录")
    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> params) {
        log.debug("=====进入登录方法=====");
        log.debug("username = " + params.get("username") + ", password = " + params.get("password"));
        // 进行登录操作 TODO
        // 响应数据
        // // {"flag": ture, "message": "登录成功", "data": {"token":"admin"}  }
        // {"flag": ture, "message": "登录成功", "data": "admin"  }
        Map<String, String> map = new HashMap<>();
        map.put("token", params.get("username"));
        return toDataResult(map);
    }

    @ApiOperation("模拟获取用户信息")
    @GetMapping("/info")
    public Result info(String token) {
        log.debug("=====进入获取用户信息的方法=====");
        log.debug("token = " + token);
        // 根据token获取用户的数据 TODO
        // 返回数据
        // // {"falg":true, "message":"", "data": {"name":"", "avatar":"", "role": []} }
        Map<String, Object> map = new HashMap<>();
        map.put("name", token);
        map.put("avatar", "https://img1.baidu.com/it/u=2838100141,2488760005&fm=253&app=138" +
                "&size=w931&n=0&f=JPEG&fmt=auto?sec=1663520400&t=7091611ba73832e3566f237c4e9a09ed");
        map.put("roles", new String[]{"admin", "teacher", "student"});
        return toDataResult(map);
    }


    @ApiOperation("模拟退出")
    @PostMapping("/logout")
    public Result logout() {
        log.debug("=====进入退出方法=====");
        return success();
    }




    @GetMapping("/team/{id}")
    @ApiOperation("根据团队id查询一条数据")
    public Result queryByTeamId(@PathVariable Integer id){
        //转Long
        Long id1 = Long.valueOf(id);

        List<Doctor> doctorList = doctorService.getByTeamId(id);
        return toDataResult(doctorList);
    }

    /**
     * 根据id查看详情
     */
    @GetMapping("/detail/{id}")
    @ApiOperation("根据id查看详情")
    public Result queryDetailById(@PathVariable Long id){
        DoctorDetailVo doctorDetailVo= doctorService.queryDetailById(id);
        return toDataResult(doctorDetailVo);
    }

    @GetMapping
    @ApiOperation("查询所有医生")
    public Result searchAll(){
        List<Doctor> list = doctorService.list();

        return toDataResult(list);
    }

}
